{extends file="./main.tpl"}
{block name=head}
<script type="text/javascript">
$(document).ready(function() {
  $('#itemId').focus();
  $(".amount").blur(function()
  {
    if($(".amount:last").val() != '' && $(".amount:last").val() > 0)
    {
      $("#one").clone(true).appendTo("#mainDiv")
      .find('input[type="text"]').val('').end()
      .find('select[option=""]').val('');
      var rowCount = $('#mainDiv tr').length;
      $(".srNo:last").val(rowCount);
    }
  });
});

function setUnit(thisObj)
{
	var row    = $(thisObj).parents('.itemRow');
	var itemId = row.find('.itemId').val() != '' ? row.find('.itemId').val() : 0;
	var dataString = "itemId=" +itemId;
	$.ajax({
	  type: "GET",
	  url: "setUnit.php",
	  data: dataString,
	  success:function(data)
	  {
	  	row.find('.getUnit').val(data);
    }
  });
}
function setAmount(amountObj)
{
	var row = $(amountObj).parents('.itemRow');
	var quantity = row.find('.quantity').val() != '' ? row.find('.quantity').val() : 0;
	var rate     = row.find('.rate').val() != '' ? row.find('.rate').val() : 0;
	var amount      = 0;
	var totalAmount = 0;

	amount = (parseFloat(quantity) * parseFloat(rate));
  row.find('.amount').val(amount.toFixed(2));
  
  $('.amount').each(function(i)
  {
    amount = $(this).val();
    if(!isNaN(amount))
    {
    	totalAmount += Number(amount);
    }
  });
  $('#totalAmount').val(totalAmount.toFixed(2));
}
</script>
{/block}
{block name=body}
<form name="salesForm"action="{$smarty.server.PHP_SELF}" method="POST" onsubmit="return confirm('DO You Want To Submit !!!!!')";>
<input type="hidden" name="status" value="{$status}" />
<table cellpadding="2" cellspacing="0" border="" align="center">
  <tr>
    <td colspan="6" style="background:#EBF5FF;">
    	{if $status == 'S'}
        <h1 align="center"><font size="5px">Sales</font></h1>
      {/if}
      {if $status == 'SR'}
        <h1 align="center"><font size="5px">Sales Return</font></h1>
      {/if}
    </td>
  </tr>
  <tr>
  	<td align="left"><b>Chalan No :</b>
  	  <input type="text" name="chalanNo" size="10" />
    </td>
    <td align="right" colspan="2"><b>Party Name :</b>
    	<select name="partyId" REQUIRED >
    		<option value=''>Select</option>
  		  {html_options values=$partyArray.partyId output=$partyArray.partyName}
      </select>
    </td>
    <td align="left" colspan="2"><b>Date :</b>
      {html_select_date prefix="salesDate" start_year="-1" end_year="+0" field_order="DMY" day_value_format="%02d"}
    </td>
    <td align="left"><b>Order No :</b>
  	  <input type="text" name="orderNo" size="10" />
    </td>
  </tr>
  <tr>
    <th>Sr No.</th>
    <th>Item</th>
    <th>Unit</th>
    <th>Quantity</th>
    <th>Rate</th>
    <th>Amount</th>
  </tr>
  <tbody id="mainDiv">
    <tr id="one" class="itemRow">
      <td align="center">
        <input type="text" class="srNo" size="3" value="1" style="text-align:center;" DISABLED />
      </td>
      <td>
        <select name="itemId[]" class="itemId" onchange="setUnit(this);">
          <option value="0">Select Item</option>
          {html_options values=$itemArray.itemId output=$itemArray.itemName}
        </select>
      </td>
      <td>
    	  <input type="text" name="unit[]" class="getUnit" DISABLED />
      </td>
      <td>
    	  <input type="text" name="quantity[]" class="quantity" onblur="setAmount(this);" />
      </td>
      <td>
    	  <input type="text" name="rate[]" class="rate" onblur="setAmount(this);" />
      </td>
      <td>
    	  <input type="text" name="amount[]" class="amount" READONLY />
      </td>
    </tr>
  </tbody>
  <tr>
    <th colspan="5" align="right">Total : </th>
    <td>
      <input type="text" name="totalAmount" id="totalAmount" DISABLED />
    </td>
  </tr>
  <tr>
  	<td colspan="6" align="center"><input type="submit" value="Submit" name="submit"></td>
  </tr>
</table>
</form>
{/block}